package com.zhao.dota.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhao.dota.mapper.PermissionMapper;
import com.zhao.dota.mapper.RolePermissionMapper;
import com.zhao.dota.model.Permission;
import com.zhao.dota.model.RolePermission;
import com.zhao.dota.service.PermissionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

    @Resource
    RolePermissionMapper rolePermissionMapper;

    @Override
    public List<Permission> tree() {
        List<Permission> list = list();
        return treeify(list);
    }

    private List<Permission> treeify(List<Permission> list) {
        Map<Integer, List<Permission>> collect = list.stream().collect(Collectors.groupingBy(Permission::getParentId));
        for (Permission p : list) {
            p.setChildren(collect.get(p.getId()));
        }
        return collect.get(0);
    }

    @Override
    public List<Permission> treeByRole(Integer roleId) {
        List<Permission> list = list();
        List<Permission> selections = baseMapper.findByRole(roleId);
        Map<Integer, Integer> idMap = selections.stream().collect(Collectors.toMap(Permission::getId, Permission::getId));
        list.forEach(p -> {
            if (idMap.containsKey(p.getId())) {
                p.setSelected(true);
            }
        });
        return treeify(list);
    }

    @Override
    public List<Permission> rolePermissions(Integer roleId) {
        return baseMapper.findByRole(roleId);
    }

    @Override
    public int saveRolePermissions(Integer roleId, Integer[] permissions) {
        List<Permission> selections = baseMapper.findByRole(roleId);
        Map<Integer, Integer> idMap = selections.stream().collect(Collectors.toMap(Permission::getId, Permission::getId));
        int count = 0;
        for (Integer id : permissions) {
            Integer remove = idMap.remove(id);
            if (remove == null) {
                RolePermission rolePermission = new RolePermission();
                rolePermission.setRoleId(roleId);
                rolePermission.setPermissionId(id);
                count += rolePermissionMapper.insert(rolePermission);
            }
        }
        Collection<Integer> delete = idMap.values();
        if (!delete.isEmpty()) {
            LambdaQueryWrapper<RolePermission> query = Wrappers.lambdaQuery(RolePermission.class).in(RolePermission::getPermissionId, delete);
            List<RolePermission> rolePermissions = rolePermissionMapper.selectList(query);
            List<Integer> ids = rolePermissions.stream().map(RolePermission::getId).collect(Collectors.toList());
            count += rolePermissionMapper.deleteBatchIds(ids);
        }
        return count;
    }


}
